#!/bin/bash

# 提示用户输入要放行的端口
read -p "请输入要放行的端口号: " PORT

# 检查iptables是否安装
if ! command -v iptables &> /dev/null
then
    echo "iptables not found. Please make sure iptables is installed."
    exit 1
fi

# 添加防火墙规则放行TCP端口
echo "Opening TCP port $PORT..."
/sbin/iptables -A INPUT -p tcp --dport $PORT -j ACCEPT
/sbin/iptables -A OUTPUT -p tcp --sport $PORT -j ACCEPT

# 如果是Ubuntu或Debian系统，并且使用了ufw防火墙，还需要更新ufw规则
if command -v ufw &> /dev/null
then
    echo "Updating ufw rules for TCP port $PORT..."
    /usr/sbin/ufw allow $PORT/tcp
fi

echo "Port $PORT has been opened."


# 提醒用户保存iptables规则以防止重启后丢失
echo "Please remember to save the iptables rules with 'sudo iptables-save' if needed."

exit 0